package com.medical.standard.service;

import com.medical.standard.dao.ExportDao;
import com.medical.standard.util.ExcelExportUtil;
import com.medical.standard.util.WordExportUtil;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

public class ExportService {

    private ExportDao exportDao;

    public ExportService() {
        this.exportDao = new ExportDao();
    }

    /**
     * 导出能力建设信息网站项目表
     */
    public void exportCapacityTable(HttpServletResponse response, Map<String, Object> filters) {
        try {
            String standardCode = (String) filters.get("standardCode");
            String projectName = (String) filters.get("projectName");

            List<Map<String, Object>> dataList = exportDao.selectCapacityTableData(standardCode, projectName);

            String[] headers = {"项目名称", "标准编号", "项目条款"};
            String[] fieldNames = {"projectName", "standardCode", "projectNum"};

            ExcelExportUtil.exportToExcel(dataList, headers, fieldNames, "能力建设信息网站项目表", response);

        } catch (Exception e) {
            throw new RuntimeException("导出能力建设信息网站项目表失败: " + e.getMessage(), e);
        }
    }

    /**
     * 导出认可检验检测能力表
     */
    public void exportInspectionCapacityTable(HttpServletResponse response, Map<String, Object> filters) {
        try {
            String standardCode = (String) filters.get("standardCode");
            String projectName = (String) filters.get("projectName");

            List<Map<String, Object>> dataList = exportDao.selectInspectionCapacityData(standardCode, projectName);

            String[] headers = {"项目名称", "标准名称", "标准编号+项目条款", "使用设备"};
            String[] fieldNames = {"projectName", "standardName", "standardItem", "equipmentName"};

            ExcelExportUtil.exportToExcel(dataList, headers, fieldNames, "认可检验检测能力表", response);

        } catch (Exception e) {
            throw new RuntimeException("导出认可检验检测能力表失败: " + e.getMessage(), e);
        }
    }

    /**
     * 导出操作规程表 - 支持动态字段选择
     */
    public void exportOperationProcedureTable(HttpServletResponse response,
                                              Map<String, Object> filters,
                                              List<String> selectedFields) {
        try {
            String standardCode = (String) filters.get("standardCode");
            String projectName = (String) filters.get("projectName");

            List<Map<String, Object>> dataList = exportDao.selectOperationProcedureData(standardCode, projectName);

            // 动态字段处理
            if (selectedFields == null || selectedFields.isEmpty()) {
                selectedFields = Arrays.asList("standardCode", "standardName", "projectNum",
                        "projectName", "projectRequirement", "equipment",
                        "itemMethod", "note");
            }

            String[] headers = getHeadersBySelectedFields(selectedFields);
            String[] fieldNames = selectedFields.toArray(new String[0]);

            ExcelExportUtil.exportToExcel(dataList, headers, fieldNames, "操作规程表", response);

        } catch (Exception e) {
            throw new RuntimeException("导出操作规程表失败: " + e.getMessage(), e);
        }
    }

    /**
     * 导出产品技术要求Word文档
     */
    public void exportProductRequirement(HttpServletResponse response,
                                         Map<String, Object> requirementData) {
        try {
            WordExportUtil.exportProductRequirementDoc(response, requirementData);
        } catch (Exception e) {
            throw new RuntimeException("导出产品技术要求失败: " + e.getMessage(), e);
        }
    }

    /**
     * 标准内容搜索
     */
    public List<Map<String, Object>> searchStandardContent(String keyword) {
        try {
            return exportDao.searchStandardContent(keyword);
        } catch (Exception e) {
            throw new RuntimeException("搜索标准内容失败: " + e.getMessage(), e);
        }
    }

    private String[] getHeadersBySelectedFields(List<String> selectedFields) {
        Map<String, String> headerMapping = new HashMap<>();
        headerMapping.put("standardCode", "标准编号");
        headerMapping.put("standardName", "标准名称");
        headerMapping.put("projectNum", "项目条款");
        headerMapping.put("projectName", "项目名称");
        headerMapping.put("projectRequirement", "项目要求");
        headerMapping.put("equipment", "使用设备");
        headerMapping.put("itemMethod", "项目方法");
        headerMapping.put("note", "注意事项");

        return selectedFields.stream()
                .map(field -> headerMapping.getOrDefault(field, field))
                .toArray(String[]::new);
    }
    // 在ExportService中添加数据验证
    public void validateExportData(Map<String, Object> filters) {
        List<Map<String, Object>> data = exportDao.selectOperationProcedureData(
                (String) filters.get("standardCode"),
                (String) filters.get("projectName")
        );

        for (Map<String, Object> record : data) {
            if (record.get("projectRequirement") == null) {
                System.out.println("警告: 项目要求为空 - " + record.get("projectNum"));
            }
            if ("待配置".equals(record.get("equipment"))) {
                System.out.println("警告: 设备未配置 - " + record.get("projectName"));
            }
        }
    }
}